package org.bmp.zemail.security.services;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class CertService {
	
	public static PublicKey getPublicKey (byte[] encodedPublicKey) {
		try {
			PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(
					new X509EncodedKeySpec(encodedPublicKey));
			return publicKey;
		} catch (InvalidKeySpecException e) {
			// TODO BM problemas
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO BM problemas
			e.printStackTrace();
		}
		return null;
	}
	
	public static PrivateKey getPrivateKey (byte[] encodedPrivateKey) {
		try {
			PrivateKey privKey = KeyFactory.getInstance("RSA").generatePrivate(
					new PKCS8EncodedKeySpec(encodedPrivateKey));
			return privKey;
		} catch (InvalidKeySpecException e) {
			// TODO BM problemas
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO BM problemas
			e.printStackTrace();
		}
		return null;
	}
	
}
